<template>
  <div class="box" >
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-position="left" label-width="0px" class="demo-ruleForm login-container">
      <h3 class="title" >系统登录</h3>
      <el-form-item prop="type" align="center">
        <el-radio-group v-model="ruleForm.type" style="display: flex;justify-content: space-around;">
          <el-radio :label="0" >管理员</el-radio>
          <el-radio :label="1">普通用户</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="account">
        <el-input type="text" v-model="ruleForm.account" auto-complete="off" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" auto-complete="off" placeholder="密码"></el-input>
      </el-form-item>
      <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button type="warning" style="width:100%;" @click.native.prevent="handleSubmit" :loading="logining">登录</el-button>
        <!--<el-button type="primary" style="width:45%;" @click.native.prevent="register">公司入驻</el-button>-->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { requestLogin } from '../api/api';
  //import NProgress from 'nprogress'
  import jwt from 'jsonwebtoken';
  export default {
    data() {
      return {
        logining: false,
        ruleForm: {
          account: '东邪',
          checkPass: '123456',
          type: 0
        },
        rules: {
          account: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            //{ validator: validaePass }
          ],
          checkPass: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            //{ validator: validaePass2 }
          ]
        },
        checked: true
      };
    },
    methods: {
      register(){
          //跳转到公司入驻界面
          this.$router.push({ path: '/register' });
      },
      handleReset() {
        this.$refs.ruleForm.resetFields();
      },
      //处理登录请求
      handleSubmit() {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.logining = true;
            var loginParams = {
              username: this.ruleForm.account,
              password: this.ruleForm.checkPass,
              type: this.ruleForm.type
            };
            this.$http.post("/login", loginParams).then(res=>{
              if (res.data.success){
                let jwt = require('jsonwebtoken'); //加载jsonwebtoken工具包
                let resultJwtJson = jwt.decode(res.data.data)//解析JwtToken
                localStorage.setItem("token",res.data.data);
                localStorage.setItem("realName",resultJwtJson.payload.logininfo.username);
                localStorage.setItem("permissions",JSON.stringify(resultJwtJson.payload.permissions));
                localStorage.setItem("menus",JSON.stringify(resultJwtJson.payload.menus));
                this.$router.push({ path: '/echarts' });
                location.reload();
                console.log(resultJwtJson)
                this.$message.success(res.data.message);
              }else {
                this.$message.error(res.data.message);
              }
              this.logining = false;
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }

</script>

<style lang="scss" scoped>
  .box {
    width: 100%;
    height: 100vh; /* 使用视口高度确保全屏 */
    margin: 0;
    padding: 0;
    background-image: url('http://img.netbian.com/file/2024/0331/232324P6NLH.jpg');
    background-size: cover; /* 背景图片覆盖整个元素 */
    background-repeat: no-repeat; /* 背景图片不重复 */
    background-position: center; /* 背景图片居中显示 */
    position: fixed; /* 固定位置，确保背景图片始终覆盖整个视口 */
    top: 0;
    left: 0;
    z-index: 1; /* 确保背景图片在所有内容的后面 */
  }
  .login-container {
    position: relative; /* 相对于.box定位 */
    z-index: 999; /* 确保登录表单在背景图片之上 */
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    width: 350px;
    margin: 180px auto;
    padding: 35px 35px 15px 35px;
    background: rgba(255, 255, 255, 0.7);
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #a8998b;
    .title {
      margin: 0px auto 40px auto;
      text-align: center;
      color: #606266;
    }
    .remember {
      margin: 0px 0px 35px 0px;
    }
  }
</style>
